package com.mune.gulimall.order.interceptor;

import com.mune.common.constant.AuthServerConstant;
import com.mune.common.to.MemberSessionTo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginUserInterceptor implements HandlerInterceptor {
  public static ThreadLocal<MemberSessionTo> orderThread = new ThreadLocal<>();

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String uri = request.getRequestURI();
    AntPathMatcher antPathMatcher = new AntPathMatcher();
    boolean match = antPathMatcher.match("/order/order/status/**", uri);
    boolean match1 = antPathMatcher.match("/payed/notify", uri);
    if (match || match1) return true;

    HttpSession session = request.getSession();
    MemberSessionTo memberSessionTo = (MemberSessionTo) session.getAttribute(AuthServerConstant.LOGIN_USER);
    if (memberSessionTo != null) {
      orderThread.set(memberSessionTo);
      return true;
    } else {
      response.sendRedirect("http://auth.gulimall.com/login.html");
      return false;
    }
  }
}
